<?php

declare(strict_types=1);

namespace App\Common\Robot\Gateways;

use App\Robot\Dto\ChatRecordMessageDTO;
use App\Robot\Dto\FileMessageDTO;
use App\Robot\Dto\GifMessageDTO;
use App\Robot\Dto\ImageMessageDTO;
use App\Robot\Dto\LocationMessageDTO;
use App\Robot\Dto\MiniProgramMessageDTO;
use App\Robot\Dto\SetProfileDTO;
use App\Robot\Dto\UrlMessageDTO;
use App\Robot\Dto\VideoChannelMessageDTO;
use App\Robot\Dto\VideoMessageDTO;
use App\Robot\Dto\VoiceMessageDTO;

class WinWeWorkGateway extends Gateway
{
    public function getLoginQrCode(string $customerParam, string $proxyUrl = '', int $onlyProxyLogin = 1): mixed
    {
        $data = $this->post('/login/getLoginQrCode', [
            'key' => $this->uuid,
            'customerParam' => $customerParam,
            'uuid' => $this->uuid,
        ]);
        if($data !== false){
            $data = camelToUnder($data);
        }
        return $data;
    }

    public function checkLoginQrCode(): mixed
    {
        $data = $this->post('/login/getStatus', [
            'key' => $this->uuid,
        ]);
        return $data;
    }

    public function checkLogin(): mixed
    {
        $data = $this->post('/login/getStatus', [
            'key' => $this->uuid,
        ]);
        return $data;
    }

    public function verifyLoginQrcode(string $code,string $loginQrcodeKey = ''): mixed
    {
        $data = $this->post('/login/verifyLogin', [
            'key' => $this->uuid,
            'loginQrcodeKey' => $loginQrcodeKey,
            'verifyCode' => $code
        ]);
        return $data;
    }

    public function manualLogin(string $userId, string $customerParam = '',string $proxyUrl = '',int $onlyProxyLogin = 1, int $deviceType = 1): mixed
    {
        $data = $this->post('/login/autoLogin', [
            'key' => $this->uuid,
            'userId' => $userId,
            'customerParam' => $customerParam.'_'.$this->serverId,
            'uuid' => $this->uuid
        ]);
        return $data;
    }

    /**
     * 取消代理
     * @return mixed
     */
    public function cancelProxy(): mixed
    {
        return false;
    }


    public function loginOut(): mixed
    {
        $data = $this->post('/login/loginOut', [
            'key' => $this->uuid,
        ]);
        return $data;
    }

    public function getEmployeeList(string $currentVersion = '', int $limit = null): mixed
    {
        $data = $this->toPost('/employee/list', [
            'key' => $this->uuid,
            'current_version' => $currentVersion
        ]);
        return $data;
    }

    public function getEmployeeDetail(array $userIdList): mixed
    {
        $data = $this->toPost('/employee/detail', [
            'key' => $this->uuid,
            'contactList' => $userIdList
        ]);
        return $data;
    }

    public function getProfile(string $userId = ''): mixed
    {
        $data = $this->toPost('/contact/detail', [
            'key' => $this->uuid,
            'userIdList' => [$userId]
        ]);
        if($data !== false){
            return array_shift($data['list']);
        }
        return false;
    }

    public function setProfile(SetProfileDTO $data): mixed
    {
        $data = $this->toPost('/room/transferRoomOwner', array(['key' => $this->uuid],$data->toArray()));
        return $data;
    }

    public function getPersonalQRCode(): mixed
    {
        $data = $this->post('/workbench/personalQRCode', [
            'key' => $this->uuid,
        ]);
        return $data;
    }

    public function syncCollectionMsg(int $seq = 0, int $limit = null): mixed
    {
        return false;
    }

    public function insertCollectionMsg(array $msgData, int $type = 29): mixed
    {
        return false;
    }

    public function getCorpInfo(array $corpIdList = []): mixed
    {
        $data = $this->toPost('/common/getCorpInfo', [
            'key' => $this->uuid,
            'corpIdList' => $corpIdList
        ]);
        return $data;
    }

    public function getTagList(int $labelType, int $currentSeq = 0): mixed
    {
        $data = $this->toPost('/label/list', [
            'key' => $this->uuid,
            'labelType' => $labelType,
            'currentSeq' => $currentSeq
        ]);
        return $data;
    }

    public function editLabel(int $opType, array $paramList): mixed
    {
        $paramList = camelToUnder($paramList);
        $data = $this->toPost('/label/op', [
            'key' => $this->uuid,
            'opType' => $opType,
            'paramList' => $paramList
        ]);
        return $data;
    }

    public function contactEditLabel(int $opType, array $paramList): mixed
    {
        $paramList = camelToUnder($paramList);

        $data = $this->toPost('/label/labelMemberOp', [
            'key' => $this->uuid,
            'opType' => $opType,
            'paramList' => $paramList
        ]);
        return $data;
    }

    public function getContactList(int $currentSeq = 0, int $limit = null, int $bizType = null): mixed
    {
        $data = $this->toPost('/contact/list', [
            'key' => $this->uuid,
            'currentSeq' => $currentSeq
        ]);

        if($data !== false){
            $contactList = $data['list'];
            foreach ($contactList as $k => $contact){
                $contactList[$k]['avatar_url'] = $contact['avatar'];
                $contactList[$k]['corp_name'] = $contact['corp_short_name'];
                $contactList[$k]['corp_full_name'] = $contact['corp_name'];
                $contactList[$k]['contact_type'] = $contact['status'];
                if($contact['corp_id'] == '1970325134026788'){
                    $contactList[$k]['corp_id'] = 0;
                }
            }
            $data['contact_list'] = $contactList;
            unset($data['list']);
        }

        return $data;
    }

    public function searchContact(string $keyword): mixed
    {
        $data = $this->toPost('/friend/searchContacts', [
            'key' => $this->uuid,
            'keyword' => $keyword
        ]);
        return $data;
    }

    public function addSearchWxContact(string $userId, string $unionId, string $verifyText = ''): mixed
    {
        $data = $this->toPost('/friend/addSearchContacts', [
            'key' => $this->uuid,
            'unionId' => $unionId,
            'userId' => $userId,
            'verifyText' => empty($verifyText) ? '通过手机号搜索添加' : ''
        ]);
        return $data;
    }

    public function addSearchWxWorkContact(string $userId, string $corpId, string $ticket, string $verifyText = ''): mixed
    {
        $data = $this->toPost('/friend/addSearchContacts', [
            'key' => $this->uuid,
            'corpId' => $corpId,
            'userId' => $userId,
            'ticket' => $ticket,
            'verifyText' => empty($verifyText) ? '通过手机号搜索添加' : ''
        ]);
        return $data;
    }

    public function addCardContact(string $presenterId, string $fromRoomId, string $userId, string $verifyText): mixed
    {
        return false;
    }

    public function addDeletedContact(string $userId, string $verifyText): mixed
    {
        $data = $this->toPost('/friend/addFriendNoValReq', [
            'key' => $this->uuid,
            'userId' => $userId,
        ]);
        return $data;
    }

    public function agreeContact(string $userId, string $corpId): mixed
    {
        $data = $this->toPost('/friend/agreeFriendApply', [
            'key' => $this->uuid,
            'userId' => $userId,
            'corpId' => '',
        ]);
        return $data;
    }

    public function getContactDetail(array $useridList): mixed
    {
        $data = $this->post('/contact/detail', [
            'key' => $this->uuid,
            'userIdList' => $useridList
        ]);
        if($data !== false){
            $list = $data['list'];
            foreach ($list as $k => $contact){
                $list[$k]['avatar_url'] = $contact['avatar'];
            }
            $data['contact_list'] = $list;
            unset($data['list']);
        }
        return $data;
    }

    public function updateWxContact(string $userid, string $remark, string $desc, string $corpRemark, array $mobileList,array $labelList): mixed
    {
        $data = $this->toPost('/contact/editRemarkMobile', [
            'key' => $this->uuid,
            'userId' => $userid,
            'desc' => $desc,
            'remark' => $remark,
            'companyRemark' => $corpRemark,
            'mobileList' => $mobileList,
            'labelList' => $labelList
        ]);
        return $data;
    }

    public function updateWxWorkContact(string $userid, string $remark, string $desc): mixed
    {
        $data = $this->toPost('/employee/editRemark', [
            'key' => $this->uuid,
            'userId' => $userid,
            'desc' => $desc,
            'remark' => $remark
        ]);
        return $data;
    }

    public function deleteContact(mixed $userId): mixed
    {
        $data = $this->toPost('/friend/delFriend', [
            'key' => $this->uuid,
            'userId' => $userId
        ]);
        return $data;
    }

    public function useridToOpenuserid(array $userIdList): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'userIdList' => $userIdList,
        ];
        $data = $this->toPost('/common/useridToOpenuserid', $postData);
        return !empty($data['open_user_list'])?$data['open_user_list']:[];
    }

    public function getSessionPage(int $currentSeq = 0, int $limit = null): mixed
    {
        $data = $this->toPost('/common/getSessionList', [
            'key' => $this->uuid,
            'currentSeq' => $currentSeq
        ]);
        return $data;
    }

    public function setSessionCmd(string $chatId, int $chatType): mixed
    {
        $data = $this->toPost('/message/settingChat', [
            'key' => $this->uuid,
            'chatId' => $chatId,
            'setType' => $chatType
        ]);
        return $data;
    }

    public function getSessionList(int $getFlag = null): mixed
    {
        $data = $this->toPost('/common/getChatListDetail', [
            'key' => $this->uuid,
            'getFlag' => $getFlag
        ]);
        return $data;
    }

    public function createRoom(array $memberList, int $isOuterRoom = 1): mixed
    {
        $data = $this->toPost('/room/create', [
            'key' => $this->uuid,
            'memberList'=>$memberList,
            'isOuterRoom' => $isOuterRoom
        ]);
        return $data;
    }

    public function editRoomName(mixed $roomId, string $roomName): mixed
    {
        $data = $this->toPost('/room/editRoomName', [
            'key' => $this->uuid,
            'roomId'=>$roomId,
            'name' => $roomName
        ]);
        return $data;
    }

    public function generateRoomQRCode(mixed $roomId): mixed
    {
        $data = $this->toPost('/workbench/generateRoomQRCode', [
            'key' => $this->uuid,
            'roomId' => $roomId
        ]);
        return $data;
    }

    public function transferRoomOwner(mixed $roomId, mixed $newOwnerId): mixed
    {
        $data = $this->toPost('/room/transferRoomOwner', [
            'key' => $this->uuid,
            'roomId'=> $roomId,
            'newOwnerId' => $newOwnerId
        ]);
        return $data;
    }

    public function setRoomAdmin(mixed $roomId, array $memberList, bool $isSet = true): mixed
    {
        $data = $this->toPost('/room/setRmvGrpAdmin', [
            'key' => $this->uuid,
            'roomId'=> $roomId,
            'memberList' => $memberList,
            'isSet' => $isSet
        ]);
        return $data;
    }

    public function outRoom(mixed $roomId): mixed
    {
        $data = $this->toPost('/room/exitRoom', [
            'key' => $this->uuid,
            'roomId'=>$roomId,
        ]);
        return $data;
    }

    public function removeRoomMember(mixed $roomId, array $memberList, int $isOuterRoom = 1): mixed
    {
        $data = $this->toPost('/room/removeRoomMember', [
            'key' => $this->uuid,
            'roomId' => $roomId,
            'memberList' => $memberList,
            'isOuterRoom' => $isOuterRoom
        ]);
        return $data;
    }

    public function getRoomList(int $nextStartIndex = 0, int $limit = null): mixed
    {
        $data = $this->toPost('/room/list', [
            'key' => $this->uuid,
            'nextStartIndex' => $nextStartIndex
        ]);
        return $data;
    }

    public function getRoomDetail(array $roomIdList): mixed
    {
        return $this->toPost('/room/detail', [
            'key' => $this->uuid,
            'roomIdList' => $roomIdList
        ]);
    }

    public function postGrpAnn(mixed $roomId, string $notice): mixed
    {
        $data = $this->toPost('/room/postGrpAnn', [
            'key' => $this->uuid,
            'roomId'=> $roomId,
            'notice' => $notice
        ]);
        return $data;
    }

    public function inviteFriendToRoom(string $roomId, array $memberList, int $isOuterRoom = 1): mixed
    {
        $data = $this->toPost('/room/inviteFriendToRoom', [
            'key' => $this->uuid,
            'roomId' => $roomId,
            'memberList'=>$memberList,
            'isOuterRoom' => 1
        ]);
        return $data;
    }

    /**
     * 群成员邀请确认
     * @param string $roomId
     * @param string $inviterId
     * @param string $inviteMsgServerId
     * @param string $inviteCode
     * @param array $invitedMemberList
     * @return mixed
     */
    public function confirmInvitedMemberList(string $roomId, string $inviterId, string $inviteMsgServerId, string $inviteCode, array $invitedMemberList): mixed
    {
        return false;
    }


    public function dismissRoom(mixed $roomId): mixed
    {
        $data = $this->toPost('/room/disbandGrpChat', [
            'key' => $this->uuid,
            'roomId'=> $roomId
        ]);
        return $data;
    }

    public function roomidToOpenroomid(string $roomId): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'roomId' => $roomId,
        ];
        $data = $this->toPost('/common/roomidToOpenroomid', $postData);
        return $data;
    }

    public function sendTextMsg(string $content, string $toId, array $reply = []): mixed
    {
        $data = $this->toPost('/message/sendText', [
            'key' => $this->uuid,
            'content' => $content,
            'toId' => $toId
        ]);
        return $data;
    }

    public function sendMixTxtMsg(array $content, string $toId, array $reply = []): mixed
    {
        $data = $this->toPost('/message/sndMixTxtMsg', [
            'key' => $this->uuid,
            'content' => $content,
            'toId' => $toId
        ]);
        return $data;
    }

    public function sendImageMsg(ImageMessageDTO $imageMessage): mixed
    {
        $msgData = $imageMessage->toArray();
        $msgData['fileName'] = $msgData['filename'];
        $data = $this->toPost('/message/sendImage', array_merge([
            'key' => $this->uuid
        ], $msgData));
        return $data;
    }

    public function sendGif(GifMessageDTO $gifMessageDTO): mixed
    {
        $data = $this->toPost('/message/sendGif', array_merge([
            'key' => $this->uuid
        ], $gifMessageDTO->toArray()));
        return $data;
    }

    public function sendVideoMsg(VideoMessageDTO $videoMessage): mixed
    {
        $msgData = $videoMessage->toArray();
        $msgData['fileName'] = $msgData['filename'];
        $data = $this->toPost('/message/sendVideo', array_merge([
            'key' => $this->uuid
        ],$msgData));
        return $data;
    }

    public function sendFileMsg(FileMessageDTO $fileMessageDTO): mixed
    {
        $msgData = $fileMessageDTO->toArray();
        $msgData['fileName'] = $msgData['filename'];
        $data = $this->toPost('/message/sendFile', array_merge([
            'key' => $this->uuid
        ], $msgData));
        return $data;
    }

    public function sendVoiceMsg(VoiceMessageDTO $voiceMessageDTO): mixed
    {
        $data = $this->toPost('/message/sendVoice', array_merge([
            'key' => $this->uuid
        ], $voiceMessageDTO->toArray()));
        return $data;
    }

    public function sendUrlMsg(UrlMessageDTO $urlMessageDTO): mixed
    {
        $data = $this->toPost('/message/sendUrl', array_merge([
            'key' => $this->uuid
        ], $urlMessageDTO->toArray()));
        return $data;
    }

    public function sendMiniProgram(MiniProgramMessageDTO $miniProgramMessageDTO): mixed
    {
        $data = $this->toPost('/message/sendMiniProgram', array_merge([
            'key' => $this->uuid
        ], $miniProgramMessageDTO->toArray()));
        return $data;
    }

    public function sendPersonalCard(string $sharedId, string $toId): mixed
    {
        $data = $this->toPost('/message/sendCard', [
            'key' => $this->uuid,
            'sharedId' => $sharedId,
            'toId' => $toId
        ]);
        return $data;
    }

    public function sendLocation(LocationMessageDTO $locationMessageDTO): mixed
    {
        return false;
    }

    public function sendVideoChannelMsg(VideoChannelMessageDTO $videoChannelMessageDTO): mixed
    {
        $data = $this->toPost('/message/sendVideoChannel', array_merge([
            'key' => $this->uuid
        ], $videoChannelMessageDTO->toArray()));
        return $data;
    }

    /**
     * 发送聊天记录消息
     * @param ChatRecordMessageDTO $chatRecordMessageDTO
     * @return mixed
     */
    public function sendChatRecord(ChatRecordMessageDTO $chatRecordMessageDTO): mixed
    {
        return false;
    }


    public function revokeMsg(string $chatId, int $msgServerId): mixed
    {
        $data = $this->toPost('/message/recallMessage', [
            'key' => $this->uuid,
            'chatId' => $chatId,
            'msgServerId' => $msgServerId
        ]);
        return $data;
    }


    public function upload(string $url, string $filename, int $fileType): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'fileUrl' => $url,
            'fileType' => $fileType
        ];
        $data = $this->post('/file/upload', $postData);
        return $data;
    }

    public function cdnBigUpload(string $url, string $filename, int $fileType): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'fileUrl' => $url,
            'fileType' => $fileType
        ];
        $data = $this->post('/file/upload', $postData);
        return $data;
    }

    public function wxWorkDownload(string $fileAeskey, string $fileId, int $fileSize, int $fileType,string $filename = ''): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'fileAesKey' => $fileAeskey,
            'fileId' => $fileId,
            'fileSize' => $fileSize,
            'fileName' => $filename,
            'fileType' => $fileType
        ];
        $data = $this->toPost('/file/downloadWeComDdn', $postData);
        return $data;
    }

    public function wxDownload(string $fileAeskey, string $fileAuthkey, int $fileSize, string $fileUrl, int $fileType,string $filename = ''): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'fileAesKey' => $fileAeskey,
            'fileAuthKey' => $fileAuthkey,
            'fileSize' => $fileSize,
            'fileUrl' => $fileUrl,
            'fileName' => $filename,
            'fileType' => $fileType
        ];
        $data = $this->toPost('/file/downloadWeChatDdn', $postData);
        return $data;
    }

    public function cdnToUrl(string $fileAesKey, string $fileId, string $fileMd5): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'fileAesKey' => $fileAesKey,
            'fileId' => $fileId,
            'fileMd5' => $fileMd5
        ];
        $data = $this->toPost('/file/cdnToUrl', $postData);
        return $data;
    }

    /**
     * cdn数据刷新
     * @return mixed
     */
    public function cdnRefresher(): mixed
    {
        return false;
    }


    public function getSnsRecord(int $minSnsId = 0, int $limit = null): mixed
    {
        $data = $this->toPost('/friendCircle/homePage', [
            'key' => $this->uuid,
            'minSnsId' => $minSnsId,
        ]);
        return $data;
    }

    public function getSnsDetail(array $snsIdList): mixed
    {
        $data = $this->toPost('/friendCircle/detail', [
            'key' => $this->uuid,
            '$snsIdList' => $snsIdList,
        ]);
        return $data;
    }

    public function snsUpload(mixed $file, int $fileType): mixed
    {
        $data = $this->toPost('/friendCircle/uploadFile', [
            'key' => $this->uuid,
            'fileUrl' => $file,
            'fileType' => 35
        ]);
        return $data;
    }

    public function postSns(int $postType, string $content, array $mediaList, array $linkInfo, array $visibleUserIdList = []): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'content' => $content,
        ];

        !empty($visibleUserIdList) && $postData['visibleUserIdList'] = $visibleUserIdList;
        !empty($linkInfo) && $postData['linkInfo'] = $linkInfo;
        !empty($mediaList) && $postData['mediaList'] = $mediaList;

        $data = $this->toPost('/friendCircle/postFriendCircle', $postData);
        return $data;
    }

    public function snsLike(int $snsId, int $snsStatus = 0): mixed
    {
        return false;
    }

    public function snsComment(int $snsId, string $content, int $refCommentId = 0): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'snsId' => $snsId,
            'content' => $content,
            'refCommentId' => $refCommentId
        ];
        $data = $this->toPost('/friendCircle/comment', $postData);
        return $data;
    }

    public function deleteSnsComment(int $snsId, int $commentId): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'snsId' => $snsId,
            'commentId' => $commentId
        ];
        $data = $this->toPost('/friendCircle/deleteComment', $postData);
        return $data;
    }

    public function deleteSns(int $snsId): mixed
    {
        $postData = [
            'key' => $this->uuid,
            'snsId' => $snsId,
        ];
        $data = $this->toPost('/friendCircle/delete', $postData);
        return $data;
    }

    public function dataSync(int $businessId, int $msgSeq = 0, int $limit = null): mixed
    {
        $data = $this->post('/common/dataSync', [
            'key' => $this->uuid,
            'businessId' => $businessId,
            'msgSeq' => $msgSeq
        ]);

        if($data !== false){
            $contactList =  $data['sync_multi_data'];
            foreach ($contactList as $key => $contact){
                $contactList[$key]['seq'] = $contact['current_seq'];
                $contactList[$key]['avatar_url'] = $contact['avatar'];
            }
            $result = [
                'hasMore' => false,
                'contact_list' => $contactList
            ];
            return $result;
        }
        return false;
    }

    public function sendGroupMsg(array $msgList,int $sendType,array $toIdList): mixed
    {
        $data = $this->toPost('/message/massSendViaHelper', [
            'key' => $this->uuid,
            'msgList' => $msgList,
            'sendType' => $sendType,
            'toIdList' => $toIdList
        ]);
        return $data;
    }

    public function queryGroupMsgEnabledMembers(array $userIdList,int $sendType): mixed
    {
        return [];
    }

}